import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/LoadingUtil.dart';
import 'package:panda/util/SignUtil.dart';

import 'Recharge.dart';

class FillAuthCodePage extends StatefulWidget {
  FillAuthCodePage({Key key}) : super(key: key);

  @override
  _FillAuthCodePageState createState() => _FillAuthCodePageState();
}

class _FillAuthCodePageState extends State<FillAuthCodePage> {
  String auth_code = "", token = "";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(
          '成为店长',
          style: TextStyle(
              color: black,
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
        ),
        backgroundColor: white,
        brightness: Brightness.light,
        elevation: 0.2,
        iconTheme: IconThemeData(color: black_99),
      ),
      body: new SingleChildScrollView(
          child: new Container(
              padding: EdgeInsets.only(
                  top: ScreenUtil.getInstance().setHeight(DP_100)),
              width: double.infinity,
              child: new Column(children: <Widget>[
                new Image.asset(
                  'images/app_icon.png',
                  fit: BoxFit.cover,
                  width: ScreenUtil.getInstance().setHeight(DP_150),
                  height: ScreenUtil.getInstance().setHeight(DP_150),
                ),
                Container(height: 15),
                Text('熊猫新零售',
                    style: TextStyle(
                        letterSpacing: 5,
                        decoration: TextDecoration.none,
                        color: Colors.black87,
                        fontSize: ScreenUtil.getInstance()
                            .setSp(BIG_FONT_SIZE + 10))),
                Container(height: 5),
                Text('打造有温度的电商社交平台',
                    style: TextStyle(
                        decoration: TextDecoration.none,
                        color: Colors.black54,
                        fontSize:
                            ScreenUtil.getInstance().setSp(MINI_FONT_SIZE))),
                Container(
                  margin:
                      EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_60)),
                  padding: EdgeInsets.only(left: 15, right: 15),
                  decoration: BoxDecoration(
                      border: Border.all(color: grey, width: 1),
                      borderRadius: BorderRadius.circular(5)),
                  child: new TextField(
                    cursorColor: mainColor,
                    keyboardAppearance: Brightness.light,
                    style: TextStyle(color: Colors.black54, fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE)),
                    onChanged: (value) {
                      auth_code = value;
                    },
                    decoration: new InputDecoration(
                        hintText: "请输入推荐人的邀请码",
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: Colors.black38,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  ),
                ),
                new Container(
                  width: double.infinity,
                  margin: EdgeInsets.fromLTRB(
                      ScreenUtil.getInstance().setHeight(DP_60),
                      0,
                      ScreenUtil.getInstance().setHeight(DP_60),
                      0),
                  height: ScreenUtil.getInstance().setWidth(DP_90),
                  decoration: new BoxDecoration(
                      color: mainColor,
                      borderRadius: BorderRadius.circular(DP_80)),
                  padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                  child: new FlatButton(
                    child: new Padding(
                      padding: EdgeInsets.all(0.0),
                      child: new Text(
                        "确定提交",
                        style: new TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE)),
                      ),
                    ),
                    onPressed: () {
                      showDialog(
                          context: context,
                          barrierDismissible: false,
                          builder: (_) {
                            return new NetLoadingDialog(
                              loadingText: "提交中...",
                              bgColor: Color.fromARGB(255, 0, 0, 0),
                              requestCallBack: null,
                              outsideDismiss: false,
                            );
                          });
                      register();
                      // _checkSub(context);
                    },
                  ),
                )
              ]))),
    );
  }

  register() async {
    Map map = new Map();
    map['token'] = token;
    map['auth_code'] = auth_code;
    map['sign_type'] = SignType.USER_GROUP_RECHARGE;
    Response<String> response =
        await HttpUtil().post(Api.USER_GROUP_RECHARGE, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    if (code == 1 && mounted) {
      // Navigator.of(context).pop();
      Navigator.of(context)
          .push(new MaterialPageRoute(builder: (BuildContext context) {
        return new RechargePage(
          order_id: userMap['data']['order_id'],
          order_num: userMap['data']['order_num'],
        );
      }));
    } else {
      Fluttertoast.showToast(msg: msg);
    }
    return true;
  }
}
